package com.example.spider.task.limit;

import java.util.Arrays;
import java.util.Map;
import java.util.function.Function;

/**
 *
 * @author lym
 */
public enum LimitStrategyEnum {

    FIX_WINDOW(FixWindowLimit.class, FixWindowLimit::new),

    MAX_BYTES(MaxBytesLimit.class, MaxBytesLimit::new),

    MAX_CURRENT_REQUEST(MaxCurrentRequestLimit.class, MaxCurrentRequestLimit::new),

    NO_LIMIT(NoLimit.class, m -> NoLimit.getInstance()),

    ;

    private final Class<? extends LimitStrategy> type;

    private final Function<Map<String, Object>, LimitStrategy> parseFunction;

    LimitStrategyEnum(Class<? extends LimitStrategy> type, Function<Map<String, Object>, LimitStrategy> parseFunction) {
        this.type = type;
        this.parseFunction = parseFunction;
    }

    public static LimitStrategyEnum valueOfType(String type) {
        return Arrays.stream(values()).filter(e -> e.name().equals(type)).findFirst().orElse(NO_LIMIT);
    }

    public static LimitStrategyEnum valueOfType(Class<? extends LimitStrategy> type) {
        return Arrays.stream(values()).filter(e -> e.type.equals(type)).findFirst().orElse(NO_LIMIT);
    }

    public Class<? extends LimitStrategy> getType() {
        return type;
    }

    public Function<Map<String, Object>, LimitStrategy> getParseFunction() {
        return parseFunction;
    }

}